package com.yidu.qianfeng.track_ying;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yidu.qianfeng.common.bean.company.EmpInfo;
import com.yidu.qianfeng.common.bean.track.TrackWaybillCondition;
import com.yidu.qianfeng.common.bean.track.TrackWaybillWrap;
import com.yidu.qianfeng.common.bean.transport.TrackWaybill;
import com.yidu.qianfeng.common.bean.waybill.Waybill;
import com.yidu.qianfeng.common.util.LayuiJsonWrap;
import com.yidu.qianfeng.common.util.PrimaryKeyUtil;
import com.yidu.qianfeng.mapper.SubstationMapper;
import com.yidu.qianfeng.mapper.TrackWaybillMapper;
import com.yidu.qianfeng.mapper.WaybillMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: 银宇豪
 * @Description: 跟踪记录业务，查询
 * @Date Created in 9:40 2021/4/21
 * @since 1.0
 */
public class TrackYingService {

    //注入运单数据映射
    @Resource(name = "trackWaybillMapper")
    private TrackWaybillMapper trackWaybillMapper;

    //注入运单数据映射
    @Resource(name = "waybillMapper")
    private WaybillMapper waybillMapper;

    //注入分站数据映射
    @Resource(name = "substationMapper")
    private SubstationMapper substationMapper;

    /**
     * 生成运单状态2，预装车
     * @param waybill 运单实体
     * @param loginEmpInfo 当前登录账户
     * @return 生成跟踪主键
     */
    public String generateTrackPreLoadCar(Waybill waybill, EmpInfo loginEmpInfo) {
        //跟踪记录
        TrackWaybill trackWaybill = null;
        try {
            trackWaybill = generateTrackWaybillEntity(waybill,loginEmpInfo);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        //跟踪类型
        trackWaybill.setTrackType(2);
        //2:准备装车  准备装车时
        trackWaybill.setTrackStatus("运单号："+waybill.getWaybillConsignNum()+"准备装车");
        //新增跟踪记录
        trackWaybillMapper.insert(trackWaybill);

        return trackWaybill.getTrackId();
    }

    /**
     *  根据运单生成跟踪记录实体
     * @param waybill 运单实体 ！必须带托运单号
     * @return 生成的实体，包含录入站点、主键、托运单号、跟踪日期
     */
    private TrackWaybill generateTrackWaybillEntity(Waybill waybill,EmpInfo empInfo) throws Exception {
        if (waybill.getWaybillConsignNum() == null || waybill.getWaybillConsignNum() == ""){
            throw new Exception("ERROR: 运单托运单号不能为空!---");
        }
        TrackWaybill trackWaybill = new TrackWaybill();

        //跟踪记录录入站点
        trackWaybill.setTrackRecordSubst(empInfo.getEmpSubstId());
        //生成跟踪主键
        trackWaybill.setTrackId(PrimaryKeyUtil.generateTrackKey(waybill.getWaybillConsignNum()));
        //跟踪表记录的托运单号
        trackWaybill.setTrackConsignNum(waybill.getWaybillConsignNum());
        //生成跟踪日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        trackWaybill.setTrackDate(formatter.format(new Date()));

        return trackWaybill;
    }

    /**
     * 查询跟踪记录分页
     * @param page 当前页
     * @param limit 显示条目数
     * @return layui包装实体
     */
    public LayuiJsonWrap queryTrackLimitLayui(Integer page, Integer limit) {
        int offset = (page-1)*limit;//计算偏移量
        List<TrackWaybillWrap> trackWaybillWraps = trackWaybillMapper.selectTrackLimit(offset, limit);

        return new LayuiJsonWrap(trackWaybillMapper.selectCount(null), trackWaybillWraps);
    }

    /**
     * 根据id查询跟踪记录
     * @param trackId 跟踪记录id
     * @return 运单实体
     */
    public TrackWaybill queryOneTrackById(String trackId) {
        return trackWaybillMapper.selectById(trackId);
    }

    /**
     * 查询运单记录根据条件
     * @param trackWaybillCondition 条件包装类
     * @return layui包装实体
     */
    public LayuiJsonWrap queryTrackByConditionLayui(TrackWaybillCondition trackWaybillCondition) {
        //计算并set偏移量
        trackWaybillCondition.setOffset((trackWaybillCondition.getPage()-1) * trackWaybillCondition.getCount());
        //根据条件分页查询
        List<TrackWaybillWrap> trackWaybillWraps = trackWaybillMapper.selectTrackByCondition(trackWaybillCondition);
        //包装成layui格式并返回
        return new LayuiJsonWrap(trackWaybillMapper.selectTrackCountByCondition(trackWaybillCondition), trackWaybillWraps);

    }


}
